package com.hghivln.controller.system;

import com.hghivln.aspectj.annotation.Log;
import com.hghivln.aspectj.enums.BusinessType;
import com.hghivln.constants.Constants;
import com.hghivln.pojo.dto.MenuDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.MenuService;
import com.hghivln.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("system/menu")
public class MenuController {
    
    @Autowired
    private MenuService menuService;

    /**
     * 分页查询
     */
    @GetMapping("listAllMenus")
    public AjaxResult listAllMenus(MenuDto menuDto) {
        return menuService.listAllMenus(menuDto);
    }


    /**
     * 添加
     */
    @PostMapping("addMenu")
    @Log(title = "添加菜单",businessType = BusinessType.INSERT)
    public AjaxResult addMenu(@Validated MenuDto menuDto) {
        menuDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return menuService.addMenu(menuDto);
    }

    /**
     * 修改
     */
    @PutMapping("updateMenu")
    @Log(title = "修改菜单",businessType = BusinessType.UPDATE)
    public AjaxResult updateMenu(@Validated MenuDto menuDto) {
        menuDto.setSimpleUser(ShiroUtils.getCurrentSimpleUser());
        return menuService.updateMenu(menuDto);
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteMenuById/{id}")
    @Log(title = "删除菜单",businessType = BusinessType.DELETE)
    public AjaxResult deleteMenuById(@PathVariable @Validated @NotNull(message = "要删除的菜单ID不能为空") Long id) {
        if (menuService.hasChildByMenuId(id)) {
            return AjaxResult.fail("当前的菜单有子节点，请先删除子节点");
        }
        return menuService.deleteMenuById(id);
    }

    /**
     * 根据ID查询一条菜单信息
     */
    @GetMapping("getMenuById/{id}")
    public AjaxResult getMenuById(@PathVariable @Validated @NotNull(message = "菜单ID不能为空") Long id) {
        return menuService.getMenuById(id);
    }

    /**
     * 根据角色ID查询菜单权限ID数据
     */
    @GetMapping("getMenuIdsByRoleId")
    public AjaxResult getMenuIdsByRoleId(Long roleId) {
        return menuService.getMenuIdsByRoleId(roleId);
    }

    /**
     * 查询菜单的下拉树
     */
    @GetMapping("selectMenuTree")
    public AjaxResult selectMenuTree() {
        MenuDto menuDto = new MenuDto();
        menuDto.setStatus(Constants.STATUS_TRUE);
        return menuService.listAllMenus(menuDto);
    }
}
